﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RuleCode.Table;

namespace chatblog.bbsmanage
{
    public partial class cata : System.Web.UI.Page
    {
        string ParentID = "1";
        cbcata c;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                BindTree();
            }
        }
        private void BindTree()
        {
            
            cbarea area;
            area = cbarea.Find(" cbuserid = '" + AliApi.appUserId + "'"
                );
            if (area.Count < 1)
            {
                MessageBox.Show(this.Page,"请先创建站点.");
                Response.Redirect(ResolveUrl("~/bbsmanage/newsiteapp.aspx"));
            }
            ParentID = area.cbcataid.ToString();
            TreeView1.Nodes.Clear();
            c = cbcata.Find("( cbstatus = '" + area.cbareaid.ToString()
                + "')");
            Label_id.Text = c.cbcataid.ToString();
            Label_name.Text = c.cbname.ToString();
            TextBox_name.Text = c.cbname.ToString();
            TextBox_desc.Text = c.jcdesc.ToString();
            string filter = " cbcataid ='" + ParentID
                + "'";
            DataRow[] dRows = c.Select(filter);
            foreach (DataRow dRow in dRows)
            {
                TreeNode childNode;
                childNode = new TreeNode();
                childNode.Value = dRow["cbcataid"].ToString();
                childNode.Text = dRow["cbcataid"].ToString() + " " + dRow["cbname"].ToString() + " " + dRow["paixu"].ToString();
                int childCount = BindTreeNode(childNode.ChildNodes, dRow["cbcataid"].ToString());
                if (childCount > 0)
                    TreeView1.Nodes.AddAt(0, childNode);
                else
                    TreeView1.Nodes.Add(childNode);
            }
            TreeView1.ExpandAll();
            if (Session["pathsdf3e8ujd2"]!=null)
            {
                TreeNode tn =
                TreeView1.FindNode(Session["pathsdf3e8ujd2"].ToString());
                if (tn != null)
                {
                    c = cbcata.Find(" cbcataid = '" + tn.Value + "'");
                    Label_id.Text = c.cbcataid.ToString();
                    Label_name.Text = c.cbname.ToString();
                    TextBox_name.Text = c.cbname.ToString();
                    TextBox_desc.Text = c.jcdesc.ToString();
                    TextBox1.Text = c.paixu.ToString();
                }
            }
            
        }

        private int BindTreeNode(TreeNodeCollection nodeCollection, string parentNodeId)
        {
            string filter = " pid1='" + parentNodeId
                + "'";
            DataRow[] dRows = c.Select(filter);
            int count = 0;

            foreach (DataRow dRow in dRows)
            {
                count++;
                TreeNode childNode;
                childNode = new TreeNode();
                childNode.Value = dRow["cbcataid"].ToString();
                childNode.Text = dRow["cbcataid"].ToString() + " " + dRow["cbname"].ToString();

                int childCount = BindTreeNode(childNode.ChildNodes, dRow["cbcataid"].ToString());
                if (childCount > 0)
                    nodeCollection.AddAt(0, childNode);
                else
                    nodeCollection.Add(childNode);
            }
            return count;
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (this.TreeView1.SelectedNode != null)
            {
                cbcata c = cbcata.Find(" cbcataid = '" + this.TreeView1.SelectedNode.Value + "'");
                Label_id.Text = c.cbcataid.ToString();
                Label_name.Text = c.cbname.ToString();
                TextBox_name.Text = c.cbname.ToString();
                TextBox_desc.Text = c.jcdesc.ToString();
                TextBox1.Text = c.paixu.ToString();
                Session["pathsdf3e8ujd2"] = TreeView1.SelectedNode.ValuePath;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            cbcata c = cbcata.Find(" cbcataid = '" + Label_id.Text + "'");
            if (c.Count>0)
            {
                c.cbcataid.Value = Label_id.Text;
                c.cbname.Value = TextBox_name.Text;
                c.jcdesc.Value = TextBox_desc.Text;
                c.paixu.Value = zfunction.str2int(TextBox1.Text);
                c.Modify();
                BindTree();
                MessageBox.Show(this.Page, "操作成功");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            cbcata c = cbcata.Find(" 1=2 " );
            c.cbcataid.Value = c.GetRecID().ToString();
            c.pid1.Value = Label_id.Text;
            c.cbname.Value = TextBox_name.Text;
            c.jccreatedate.Value = DateTime.Now;
            c.createuserid.Value = AliApi.appUserId;
            c.paixu.Value = zfunction.str2int(TextBox1.Text);
            cbarea area;
            area = cbarea.Find(" cbuserid = '" + AliApi.appUserId + "'"
                );
            c.cbstatus.Value = area.cbareaid.ToString();
            c.jcdesc.Value = TextBox_desc.Text;
            cbcata c1 = cbcata.Find(" cbcataid = '" + Label_id.Text + "'");
            c.cblayer.Value = c1.cblayer.Value + 1;
            c.pid2.Value = c1.pid1.ToString();
            c.pid3.Value = c1.pid2.ToString();
            c.pid4.Value = c1.pid3.ToString();
            c.Insert();
            BindTree();
            MessageBox.Show(this.Page, "操作成功");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            BindTree();
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            cbcata c = cbcata.Find(" cbcataid = '" + Label_id.Text + "'");
            if (c.Count > 0)
            {
                cbcata c1 = cbcata.Find(" pid1 = '" + Label_id.Text + "'");
                if (c1.Count > 0)
                {
                    MessageBox.Show(this.Page, "存在子分类,不可删除.");
                    return;
                }
                cblist l = cblist.Find(" cbcataid = '" + Label_id.Text + "'");
                if (l.Count > 0)
                {
                    MessageBox.Show(this.Page, "该分类下面有帖子,不可删除.");
                    return;
                }
                c.Delete();
                BindTree();
                MessageBox.Show(this.Page, "操作成功");
            }
        }
    }
}
